package com.zrrd.stream;

import java.util.*;
import java.util.stream.Stream;

/**
 * @author WanJl
 * @version 1.0
 * @title StreamDemo03
 * @description 中间操作
 * @create 2024/6/1
 */
public class StreamDemo03 {
    public static void main(String[] args) {
        //Collection体系的集合可以使用默认的方法stream()生成流
        ArrayList<String> list = new ArrayList<>();
        list.add("张三丰");
        list.add("张无忌");
        list.add("张翠山");
        list.add("王二麻子");
        list.add("张良");
        list.add("谢广坤");
        Stream<String> listStream = list.stream();

        listStream
                .filter(s->s.length()>=3)    //过滤集合中元素长度大于3的
                .filter(s->s.startsWith("张"))//过滤集合中元素第一个字符为张的
                .limit(2)//获取前2个元素
                .forEach(System.out::println);//遍历集合
        System.out.println("------------------------------------------------");

        HashSet<String> hashSet = new HashSet<>();
        hashSet.add("郭靖");
        hashSet.add("黄蓉");
        hashSet.add("洪七公");
        hashSet.add("老顽童");
        hashSet.add("欧阳锋");
        hashSet.add("一灯大师");
        hashSet.add("王重阳");

        //Stream<String> hashSetStream = hashSet.stream();
        hashSet.stream()
                .filter(s->s.length()>=3)
                .skip(2)
                .forEach(System.out::println);

        System.out.println("------------------------------------------------");

        //Map体系，就需要转换成Set，间接的生成流
        HashMap<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1,"吕布");
        hashMap.put(2,"赵云");
        hashMap.put(3,"典韦");
        hashMap.put(4,"关羽");
        hashMap.put(5,"马超");
        hashMap.put(6,"张飞");
        hashMap.put(9,"张飞");
        hashMap.put(7,"黄忠");
        hashMap.put(8,"姜维");
        hashMap.put(10,"姜维");
        Collection<String> values =  hashMap.values();
        Stream<String> limit = values.stream().limit(4);
        Stream<String> skip = values.stream().skip(3);
        Stream.concat(limit,skip)//合并两个流为一个流
                .distinct() //过滤重复元素
                .forEach(System.out::println);


        System.out.println("------------------------------------------------");
        //数组
        String[] strings1={"狄仁杰","武则天","李治","李世民"};
        String[] strings2={"李隆基","李密","贺知章","李白","白居易","杜甫","杨玉环","张小敬"};

        Stream<String> arrayStream1 = Arrays.stream(strings1);
        Stream<String> arrayStream2 = Arrays.stream(strings2);

        Stream
                .concat(arrayStream1, arrayStream2)//把两个流合并为1个流
                .forEach(System.out::println);//遍历输出


        //相同类型的多个数据可以通过Stream接口的of方法生成流
        String s1="张三";
        String s2="张三1";
        String s3="张三2";
        String s4="张三3";
        String s5="张三5";
        Stream<String> stringStream = Stream.of(s1, s2, s3, s4, s5);

        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);


    }
}
